//
//  CateTableViewController.m
//  DanTang
//
//  Created by honeyrr on 2017/10/9.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "CateTableViewController.h"
#import "DTHTableViewCell.h"

@interface CateTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) NSArray *postArr;

@end

static NSString *const CateTableViewControllerID = @"dthcell";

@implementation CateTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([DTHTableViewCell class]) bundle:nil] forCellReuseIdentifier:CateTableViewControllerID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 160;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self loadInfo];
    
}

-(void)loadInfo
{
    NSString *url = @"";
    if ([self.type isEqualToString:@"专题合集"]) {
        url = [NSString stringWithFormat:@"http://api.dantangapp.com/v1/collections/%ld/posts?gender=1&generation=1&limit=20&offset=0",self._id];
    }else if ([self.type isEqualToString:@"风格品类"]){
        url = [NSString stringWithFormat:@"http://api.dantangapp.com/v1/channels/%ld/items?limit=20&offset=0",self._id];
    }
    [[DTNetWork sharedNetWork] loadDataInfo:url parameters:nil success:^(id  _Nullable responseObject) {
        NSArray *dict = nil;
        if ([self .type isEqualToString:@"专题合集"]) {
            dict = responseObject[@"data"][@"posts"];
        }else if ([self.type isEqualToString:@"风格品类"]){
            dict = responseObject[@"data"][@"items"];
        }
        self.postArr = dict;
        [self.tableView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.postArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DTHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CateTableViewControllerID];
    if (!cell) {
        cell = [[DTHTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CateTableViewControllerID];
    }
    //取消选中样式
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *item = self.postArr[indexPath.row];
    //cell.item = item;
    cell.itemTitle.text = [self.postArr[indexPath.row] objectForKey:@"short_title"];
    [cell.itemImg setImageWithURL:self.postArr[indexPath.row][@"cover_image_url"] placeholderImage:[UIImage imageNamed:@"PlaceHolderImage_small_31x26_"]];
    [cell.itemLikesBtn setTitle:[NSString stringWithFormat:@"%@",self.postArr[indexPath.row][@"likes_count"]]forState:UIControlStateNormal];
    return cell;
}

-(NSArray *)postArr
{
    if (!_postArr) {
        _postArr = [NSArray array];
    }
    return _postArr;
}

@end
